package frobagroup.services.frontEnd.impl;

import frobagroup.entities.frontEnd.User;
import frobagroup.entities.frontEnd.UserProfile;
import frobagroup.repositories.frontEnd.UserProfileRepository;
import frobagroup.repositories.frontEnd.UserRepoeitory;
import frobagroup.services.frontEnd.UserLoginService;
import frobagroup.utils.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Created by Tinsley on 2016/7/19.
 */
@Service
public class UserLoginServiceImpl implements UserLoginService {

    @Autowired
    private UserRepoeitory userRepoeitory;
    @Autowired
    private UserProfileRepository userProfileRepository;

    @Override
    public User findByEmailOrUsername(String email, String username) {

        return userRepoeitory.findByEmailOrUsername(email,username);
    }

    @Override
    public void lastLoginTime(User user) {
        user.setLastLoginTime(new Date());

        userRepoeitory.save(user);
    }

    @Override
    public UserProfile findByUserId(String uId) {

        return userProfileRepository.findByUId(uId);
    }

    @Override
    public UserProfile signUp(User user) {
        user.setPassword(MD5.encode(user.getPassword()));
        user.setCreateTime(new Date());
        User u = userRepoeitory.save(user);
        UserProfile up = new UserProfile();
        up.setUId(u.getId());
        up.setEmail(u.getEmail());
        return userProfileRepository.save(up);

    }
}
